﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;

namespace Yuanlin.Business
{
    public class AreaListControl : YuanlinUserControl
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        protected HtmlAnchor addchild;
        protected HtmlAnchor managechild;
        private Area area;
        private int upAreaId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.UpAreaId);

        public bool RemoveAdd
        {
            get
            {
                if (ViewState["RemoveAdd"] == null)
                    return false;
                return (bool)ViewState["RemoveAdd"];
            }
            set { ViewState["RemoveAdd"] = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            list.ItemDataBound += new RepeaterItemEventHandler(list_ItemDataBound);
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                BindData();
            }
            AjaxManager.Register(this, "AjaxMethod");
        }

        protected void list_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (RemoveAdd)
                {
                    addchild = e.Item.FindControl("addchild") as HtmlAnchor;
                    addchild.Visible = false;
                    managechild = e.Item.FindControl("managechild") as HtmlAnchor;
                    managechild.Visible = false;
                }
            }
        }

        private void BindData()
        {
            if (list == null)
            {
                return;
            }

            area = BusinessManager.GetArea(upAreaId);
            if (area != null && area.UpAreaId.Value > 0)//地区有四层
            {
                area = BusinessManager.GetArea(area.UpAreaId.Value);
                if (area != null && area.UpAreaId.Value > 0)
                {
                    RemoveAdd = true;
                }
                else
                {
                    RemoveAdd = false;
                }
            }
            AreaListRequest request = new AreaListRequest();
            request.UpAreaId = upAreaId;
            request.PageIndex = pager.PageIndex;
            request.PageSize = pager.PageSize;
            EntityList areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));

            list.DataSource = areas;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(areas.TotalCount);
                currentPage.TotalRecords = (areas.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        #region Ajax Method
        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int Deletearea(int areaId)
        {
            return BusinessManager.DeleteArea(areaId);




        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int Deleteareas(string entityIds)
        {
            int state = 0;
            if (string.IsNullOrEmpty(entityIds))
            {
                return state;
            }
            string[] entityIdArray = entityIds.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int entityId = 0;
            foreach (string stringEntityId in entityIdArray)
            {
                try
                {
                    entityId = Int32.Parse(stringEntityId);
                }
                catch
                {
                    continue;
                }
                if (entityId > 0)
                {
                    if (Deletearea(entityId) == -2)
                    {
                        state = -2;
                    }
                }
            }
            return state;
        }

        #endregion

    }
}

